package com.wit.witmg.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wit.witmg.entity.TProdstat;
import com.wit.witmg.entity.TProdtask;
import com.wit.witmg.mapper.TProdstatMapper;
import com.wit.witmg.service.ITProdstatService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wit.witmg.service.ITProdtaskService;
import com.wit.witmg.util.ErrorCode;
import com.wit.witmg.util.JsonResult;
import com.wit.witmg.util.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bob
 * @since 2025-01-09
 */
@Service
@RequiredArgsConstructor
public class TProdstatServiceImpl extends ServiceImpl<TProdstatMapper, TProdstat> implements ITProdstatService {

    final ITProdtaskService taskService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object add(HttpServletRequest request, String json) {
        if(StringUtils.instance.isEmpty(json)){
            return JsonResult.error(ErrorCode.ERR_200);
        }
        int pid;//根据数组中的pid,关联TProdtask表的id，并把计划表（TProdtask）的stat改为2；
        if(json.contains("[") && json.contains("]")){
            List<TProdstat> list=JSON.parseArray(json,TProdstat.class);
            pid=list.get(0).getPid();
            boolean bl= saveBatch(list);
            if (!bl) {
                return JsonResult.error(ErrorCode.ERR_504);
            }
            if(!taskService.updateById(new TProdtask().setId(pid).setStat(2))) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//回滚
                return JsonResult.error(ErrorCode.ERR_504);
            }
            return JsonResult.success(list);
        }else {
            TProdstat t = JSON.parseObject(json, TProdstat.class);
            pid=t.getPid();
            if (t.getId() > 0) {
                boolean bl = updateById(t);
                if (!bl) {
                    return JsonResult.error(ErrorCode.ERR_200);
                }
            } else {
                boolean bl = save(t);
                if (!bl) {
                    return JsonResult.error(ErrorCode.ERR_504);
                }
                if(!taskService.updateById(new TProdtask().setId(pid).setStat(2))) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//回滚
                    return JsonResult.error(ErrorCode.ERR_504);
                }
            }
            return JsonResult.success(t);
        }
    }

    @Override
    public Object del(HttpServletRequest request, Integer id) {
        boolean bl= removeById(id);
        if(bl) {
            return JsonResult.success();
        }
        return JsonResult.error(ErrorCode.ERR_200);
    }

    @Override
    public Object get(HttpServletRequest request, Integer pid, Integer eid,Integer oid, String eqbn, String bn) {
        QueryWrapper<TProdstat> qw=new QueryWrapper<>();

        if( pid!=null){
            qw.eq("pid",pid);
            return JsonResult.success(list(qw));
        }
        if(eid!=null){
            qw.eq("eid",eid);
            return JsonResult.success(list(qw));
        }
        if(oid!=null){
            qw.eq("oid",oid);
            return JsonResult.success(list(qw));
        }
        if(eqbn!=null){
            qw.eq("eqbn",eqbn);
            return JsonResult.success(list(qw));
        }
        if(bn!=null){
            qw.eq("bn",bn);
            return JsonResult.success(list(qw));
        }
        return JsonResult.error(ErrorCode.ERR_200);
    }
}
